package com.example.hasee.zhcd.Util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;


import com.example.hasee.zhcd.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateTimePickDialogUtil implements OnDateChangedListener,
        OnTimeChangedListener {
    private DatePicker datePicker;
    private TimePicker timePicker;
    //后加的自定义按键
    private Button btn_sure;
    private Button btn_cancel;
    private AlertDialog ad;
    private String dateTime;
    private String initDateTime;
    private Activity activity;

    public DateTimePickDialogUtil(Activity activity, String initDateTime) {
        this.activity = activity;
        this.initDateTime = initDateTime;

    }

    public void init(DatePicker datePicker, TimePicker timePicker) {
        Calendar calendar = Calendar.getInstance();
        if (!(null == initDateTime || "".equals(initDateTime))) {
            calendar = this.getCalendarByInintData(initDateTime);
        } else {
            initDateTime = calendar.get(Calendar.YEAR) + "-"
                    + calendar.get(Calendar.MONTH) + "-"
                    + calendar.get(Calendar.DAY_OF_MONTH);
        }
        datePicker.init(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), this);
    }


    public AlertDialog dateTimePicKDialog(final EditText inputDate) {
        LinearLayout dateTimeLayout = (LinearLayout) activity
                .getLayoutInflater().inflate(R.layout.common_datetime, null);
        datePicker = (DatePicker) dateTimeLayout.findViewById(R.id.datepicker);
        timePicker = (TimePicker) dateTimeLayout.findViewById(R.id.timepicker);
        //后加的自定义按键
        btn_sure = (Button) dateTimeLayout.findViewById(R.id.btn_sure);
        btn_cancel = (Button) dateTimeLayout.findViewById(R.id.btn_cancel);
        init(datePicker, timePicker);
        timePicker.setIs24HourView(true);
        timePicker.setOnTimeChangedListener(this);
        //后改的
        final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(initDateTime);
        builder.setView(dateTimeLayout);
        ad = builder.create();
        btn_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                datePicker.clearFocus();
                onDateChanged(null, 0, 0, 0);
                inputDate.setText(dateTime);
                ad.dismiss();
            }
        });
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                datePicker.clearFocus();
                onDateChanged(null, 0, 0, 0);
                if (inputDate.getText().toString().equals("")) {
                    inputDate.setText("");
                }
                ad.dismiss();
            }
        });
//        builder.setPositiveButton(activity.getString(R.string.cancel), new DialogInterface.OnClickListener() {
//            public void onClick(DialogInterface dialog, int whichButton) {
//
//                datePicker.clearFocus();
//                onDateChanged(null, 0, 0, 0);
//                if (inputDate.getText().toString().equals("")) {
//                    inputDate.setText("");
//                }
//
//            }
//        });
//        builder.setNegativeButton(activity.getString(R.string.sure), new DialogInterface.OnClickListener() {
//            public void onClick(DialogInterface dialog, int whichButton) {
//                datePicker.clearFocus();
//                onDateChanged(null, 0, 0, 0);
//                inputDate.setText(dateTime);
//
//            }
//        });

        ad.show();
//        ad = new AlertDialog.Builder(activity)
//                .setTitle(initDateTime)
//                .setView(dateTimeLayout)
//                .setPositiveButton(activity.getString(R.string.cancel), new DialogInterface.OnClickListener() {
//                    public void onClick(DialogInterface dialog, int whichButton) {
//
//                        datePicker.clearFocus();
//                        onDateChanged(null, 0, 0, 0);
//                        if (inputDate.getText().toString().equals("")) {
//                            inputDate.setText("");
//                        }
//
//                    }
//                })
//                .setNegativeButton(activity.getString(R.string.sure), new DialogInterface.OnClickListener() {
//                    public void onClick(DialogInterface dialog, int whichButton) {
//                        datePicker.clearFocus();
//                        onDateChanged(null, 0, 0, 0);
//                        inputDate.setText(dateTime);
//
//                    }
//                }).show();

        return ad;
    }

    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
        onDateChanged(null, 0, 0, 0);
    }

    public void onDateChanged(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(datePicker.getYear(), datePicker.getMonth(),
                datePicker.getDayOfMonth());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        dateTime = sdf.format(calendar.getTime());
        ad.setTitle(dateTime);
    }

    private Calendar getCalendarByInintData(String initDateTime) {
        Calendar calendar = Calendar.getInstance();
        int currentYear = Integer.parseInt(initDateTime.substring(0, 4));
        int currentMonth = Integer.parseInt(initDateTime.substring(5, 7)) - 1;
        int currentDay = Integer.parseInt(initDateTime.substring(8,
                initDateTime.length()));
        calendar.set(currentYear, currentMonth, currentDay);
        return calendar;
    }

}
